package com.tjetc.view;
import com.tjetc.domain.Order;

import com.tjetc.domain.*;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.OrderApprovalService;
import com.tjetc.service.OrderService;
import com.tjetc.service.VehicleService;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class OrderApprovalManageView {
    private static Scanner scanner=new Scanner(System.in);
    private OrderService orderService= ClientProxy.getProxyInstance(OrderService.class);
    private OrderApprovalService orderApprovalService=ClientProxy.getProxyInstance(OrderApprovalService.class);
    private VehicleService vehicleService=ClientProxy.getProxyInstance(VehicleService.class);
    public void start(){
        while (true){
            System.out.println("🥰🥰~~~~~~管理员订单管理页面~~~~~~🥰🥰");
            System.out.println("1.查看所有订单:");
            System.out.println("2.查看待审核订单:");
            System.out.println("3.查看已审核的订单:");
            System.out.println("4.进行审核:");
            System.out.println("0.返回上一个页面:");
            System.out.println("请进行选择(0~4):");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    list();
                    break;
                case 2:
                    toBeReviewed();
                    break;
                case 3:
                    reviewed();
                    break;
                case 4:
                    review();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入!");
                    break;
            }
        }
    }

    /**
     * 审核订单
     */
    private void review() {
        //先列出所有订单信息，如果有订单则继续进行，无则退出
        if (toBeReviewed()) {
            System.out.println("请输入想要审核的ID号");
            String orderId = scanner.next();
            //查询是否有该订单
            Result result = orderService.selectById(orderId);
            System.out.println(result.getMsg());
            if (result.getCode() == 200) {
                Order order = (Order) result.getData();
                //审核订单
                System.out.println("请输入是否通过(Y/N)");
                String next = scanner.next();
                if (next.equalsIgnoreCase("N")) {
                    System.out.println("请输入审核未通过的理由");
                    String reason = scanner.next();
                    OrderApproval orderApproval = new OrderApproval();
                    orderApproval.setOrderId(order.getOrderId());
                    orderApproval.setAdminId(AppData.admin.getAdminId());
                    orderApproval.setApprovalDate(LocalDateTime.now());
                    orderApproval.setApprovalResult(0);
                    orderApproval.setRejectionReason(reason);
                    orderApproval.setOrder(new Order());
                    Result result1 = orderApprovalService.insert(orderApproval);
                    System.out.println(result1.getMsg());
                    Integer vehicleId = order.getVehicleId();
                    //更新车辆状态
                    Vehicle vehicle = new Vehicle();
                    vehicle.setVehicleId(vehicleId);
                    vehicle.setCurrentStatus(0);
                    vehicleService.update(vehicle);
                    orderService.delete(orderId);
                } else {
                    OrderApproval orderApproval = new OrderApproval();
                    orderApproval.setOrderId(order.getOrderId());
                    orderApproval.setAdminId(AppData.admin.getAdminId());
                    orderApproval.setApprovalDate(LocalDateTime.now());
                    orderApproval.setApprovalResult(1);
                    orderApproval.setOrder(new Order());
                    Result result1 = orderApprovalService.insert(orderApproval);
                    System.out.println(result1.getMsg());
                    orderService.update(orderId, 1);
                }
            }
        }
    }

    /**
     * 查询某用户id已经审核的订单
     */
    private void reviewed() {
        List<OrderApproval> list = orderApprovalService.reviewed(AppData.admin);
        //判断列表是否为空
        if (list==null ||list.size()==0){
            System.out.println("当前订单为空!");
        }else {
            for (OrderApproval orderApproval : list) {
                Order order = orderApproval.getOrder();
                if(order==null){
                    System.out.println("订单ID:"+orderApproval.getOrderId()+" 该订单已取消!!!");
                }else {
                    Customer customer = order.getCustomer();
                    CustomerMessage customerMessage = customer.getCustomerMessage();
                    Vehicle vehicle = order.getVehicle();
                    Integer orderStatus = order.getOrderStatus();
                    System.out.println("订单ID:"+order.getOrderId()+
                            ",客户账户:"+customer.getAccount()+
                            ",客户的真实姓名:"+customerMessage.getName()+
                            ",车辆的型号:"+vehicle.getModel()+
                            ",车辆的颜色:"+vehicle.getColor()+
                            ",车牌号:"+vehicle.getLicensePlate()+
                            ",租车开始的日期:"+order.getRentalStartDate()+
                            ",租车预计结束的日期:"+order.getRentalEndDate()+
                            ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                            ",订单预计总费用:"+order.getTotalCost()+
                            ",订单提交日期:"+order.getOrderDate());
                }
            }
        }
    }

    /**
     * 查看所有待审核的订单
     * @return
     */
    private boolean toBeReviewed() {
        List<Order> list = orderService.toBeReviewed();
        //判断是否为空
        if (list==null ||list.size()==0){
            System.out.println("当前订单为空!");
            return false;
        }else {
            for (Order order : list) {
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                Integer orderStatus = order.getOrderStatus();
                System.out.println("订单ID:"+order.getOrderId()+
                        ",客户账户:"+customer.getAccount()+
                        ",客户的真实姓名:"+customerMessage.getName()+
                        ",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                        ",租车开始的日期:"+order.getRentalStartDate()+
                        ",租车预计结束的日期:"+order.getRentalEndDate()+
                        ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                        ",订单预计总费用:"+order.getTotalCost()+
                        ",订单提交日期:"+order.getOrderDate());
            }
            return true;
        }
    }

    /**
     * 查询所有订单
     */
    private void list() {
        List<Order> list = orderService.findAll();
        //查看是否为空
        if (list==null ||list.size()==0){
            System.out.println("当前订单为空!");
        }else {
            for (Order order : list) {
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                Integer orderStatus = order.getOrderStatus();
                System.out.println("订单ID:"+order.getOrderId()+
                ",客户账户:"+customer.getAccount()+
                ",客户的真实姓名:"+customerMessage.getName()+
                        ",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                ",租车开始的日期:"+order.getRentalStartDate()+
                ",租车预计结束的日期:"+order.getRentalEndDate()+
                        ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                        ",订单预计总费用:"+order.getTotalCost()+
                        ",订单提交日期:"+order.getOrderDate());
            }
        }
    }
}
